

    cd $firmpath
	global firstyear 1996
	global lastyear 2006 
	global time_constraint "" //"drop if firstyear_panel!=firstyear_sample" in a robustness, we add a constraint to deal with left censoring 
	global time_constraint2 "drop if firstyear_sample>2004" // drop right-censored seller-buyer relationships
	global dim_decile $product iso2 // we compute decile by product categories to better fit with the model, but it could be computed by $product & iso2 
	global variant _iso2 // this is the name of the variant we consider 
    global product id_conc 
		
	use base_4decile$firstyear$lastyear, clear
    egen p=group($product)
    drop if export==0 
    g dur=length_bps
    g size=export 
    collapse (mean) size dur  , by(id_bs_continuous $product iso2) 
    g lsize=log(size)
    egen _=median(lsize), by($dim_decile)
    g lsize_= size- _  // deviation with respect to size by product X country
    egen min=pctile(lsize_), by($dim_decile)  p(1) 
    egen max=pctile(lsize_), by($dim_decile)  p(99) 
	egen c=group(iso2)
    egen decile=xtile(lsize_) , by($product) nq(10)  
    drop if lsize_<min | lsize_>max // we truncate the first and tenth deciles to be fit with the theoretical framework (see appendix) 
    save base_4decile$firstyear$lastyear$variant, replace
	
	use $correspath/corres_nc8$firstyear$lastyear, clear
	keep if year==2002
	rename nc8 nc8_2002
	tostring nc8_2002, replace 
	replace nc8_2002="0"+ nc8_2002 if length(nc8_2002)==7 
	g hs6_2002=substr(nc8_2002,1,6) 
	egen count=count(id_conc), by(id_conc)
	drop if count>30 // drop 2 id_conc that have more than 30 CN8 products (79 and 139 resp.) 
	keep id_conc hs6 
	duplicates drop id_conc hs6, force 
	save temp, replace

	use base_4decile$firstyear$lastyear$variant, clear 
	sort id_conc
	joinby id_conc using temp
	egen hs=group(hs6_2002)
	local 2 dur 
    collapse (mean) size `2' (median) med_size=size med_`2'=`2', by($product iso2 decile hs hs6_2002) 
	g l`2'=log(`2')
	g fdecile=log(log(10 - decile) - log(9 - decile))
	replace fdecile=log(log(1.1)) if decile==1 // this is for size from pctile 0.01 to pctile 0.1 
	replace fdecile=log(log(10)) if decile==10 // this is for size from pctile 0.9 to pctile 0.99 
	replace l`2'=log(`2' /0.9) if decile==1 |decile==10 // to account for the fact that we drop the top1% and bottom 1%
	corr l`2' size fdecile decile // large transactions last longer as in the model
	egen p=group($product)
	egen c=group(iso2)
	reghdfe l`2' fdecile, a(rs_hs6iso=hs c) 
	collapse (mean) rs* `2', by(hs6)
	save $rspath/rs_$firstyear$lastyear$variant, replace // Relationship stickiness (rs) measures by product are in this dataset 
	
	erase temp.dta 
		
/// ROBUSTNESS  - ESTIMATE BY COUNTRY (here we exploit the decile by ISO2-PRODUCT) 

    cd $firmpath
	global firstyear 1996
	global lastyear 2006 
	global time_constraint "" 
	global time_constraint2 "drop if firstyear_sample>2004" 
	global dim_decile $product iso2 
	
    global product id_conc 
	
	
	use $correspath/corres_nc8$firstyear$lastyear, clear
	keep if year==2002
	rename nc8 nc8_2002
	tostring nc8_2002, replace 
	replace nc8_2002="0"+ nc8_2002 if length(nc8_2002)==7 
	g hs6_2002=substr(nc8_2002,1,6) 
	egen count=count(id_conc), by(id_conc)
	drop if count>30 // drop 2 id_conc that have more than 30 CN8 products (79 and 139 resp.) 
	keep id_conc hs6 
	duplicates drop id_conc hs6, force 
	save temp, replace
	
	foreach j in DE BE IT ES GB{
	global variant _iso2 
	use base_4decile$firstyear$lastyear$variant, clear 
	keep if iso2=="`j'"
	sort id_conc
	joinby id_conc using temp
	egen hs=group(hs6_2002)
	local 2 dur 
    collapse (mean) size `2' (median) med_size=size med_`2'=`2', by($product iso2 decile hs hs6_2002) 
	g l`2'=log(`2')
	g fdecile=log(log(10 - decile) - log(9 - decile))
	replace fdecile=log(log(1.1)) if decile==1 // this is for size from pctile 0.01 to pctile 0.1 
	replace fdecile=log(log(10)) if decile==10 // this is for size from pctile 0.9 to pctile 0.99 
	replace l`2'=log(`2' /0.9) if decile==1 |decile==10 // to account for the fact that we drop the top1% and bottom 1%
	corr l`2' size fdecile decile // large transactions last longer as in the model
	egen p=group($product)
	egen c=group(iso2)
	reghdfe l`2' fdecile, a(rs_hs6`j'=hs) 
	collapse (mean) rs* , by(hs6)
	global variant `j'
	save $rspath/rs_$firstyear$lastyear$variant, replace // Relationship stickiness (rs) measures by product are in this dataset 
	}
	
	erase temp.dta 